package com.pagemodel.jquerystudy;

import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JqueryStudy
 */
@WebServlet("/jquerystudy")
public class JqueryStudy extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JqueryStudy() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("content-type","text/html;charset=UTF-8");//防止乱码
		response.setCharacterEncoding("UTF-8"); //防止乱码
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		System.out.println("id="+id+",name="+name);
//		Map<String,String> map = new HashMap<String,String>();
//        map.put("id",id);
//        map.put("name",name);
//		String js = JSON.toJSONString(map,true);//map转json格式，后面一定要加true
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		for(int i=0; i<3; i++) {
			Map<String,String> map = new HashMap<String,String>();
			map.put("id",id);
			map.put("name",name);
			list.add(map);
		}
		String js = JSON.toJSONString(list,true);//list转json格式，后面一定要加true
		response.getWriter().print(js);
	}

}
